/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.asportoonline.business.beans;

import java.util.ArrayList;
import javax.ejb.EJB;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.ejb.Stateless;

/**
 *
 * @author fe
 */
import java.util.List;
import org.asportoonline.entities.Locale;
import org.asportoonline.entities.Prodotto;
import org.asportoonline.integration.LocaleFacade;
import org.asportoonline.integration.ProdottoFacade;

/**
 *
 * @author fe
 */
@WebService(serviceName = "AsportoOnlineWS")
@Stateless()
public class AsportoOnlineWS {

    @EJB
    private LocaleFacade localeBean;
    @EJB
    private ProdottoFacade ProdottoBean;

    /** This is a sample web service operation */
    @WebMethod(operationName = "hello")
    public String hello(@WebParam(name = "name") String name) {
        return "Hello " + name + " !";
    }

    @WebMethod(operationName = "recuperaLocalita")
    public List<String> recuperaLocalita() {
        return localeBean.fetch_all_locality();
    }

    @WebMethod(operationName = "recuperaLocali")
    public List<String> recuperaLocali(@WebParam(name = "localita") String localita) {
        List<Locale> listaLocali = localeBean.find_for_locality(localita);
        List<String> nomiLocali = new ArrayList<String>();

        for (int i = 0; i < listaLocali.size(); i++) {
            nomiLocali.add(listaLocali.get(i).getNome());
        }

        return nomiLocali;
    }
    
    @WebMethod(operationName = "recuperaDettagliLocale")
    public List<String> recuperaDettagliLocale(@WebParam(name = "locale") String locale) {
        Locale l = localeBean.find(locale);
        
        String[] chiusura = {"Nessuno", "Lunedì", "Martedì", "Mercoledì", "Giovedì", "Venerdì", "Sabato", "Domenica"};
        
        List<String> ll = new ArrayList<String>();
        ll.add(l.getNome());
        ll.add(l.getIndirizzo().getVia());
        ll.add(l.getIndirizzo().getCap() + ", " + l.getIndirizzo().getCitta());
        ll.add(l.getTelefono());
        ll.add(l.getEmail());        
        ll.add(l.getPrimaApertura());
        ll.add(l.getSecondaApertura()==null ? " " : l.getSecondaApertura());
        ll.add(chiusura[l.getGiornoChiusura()+1]);
        ll.add(l.getDistanzaMassima() + "km");
        ll.add(l.getUrlimmagine());
        return ll;
    }
    
    @WebMethod(operationName = "recuperaProdotti")
    public List<String> recuperaProdotti(@WebParam(name = "locale") String locale) {
        Locale l = localeBean.find(locale);
        List<Prodotto> lp = ProdottoBean.findAllRestaurantProducts(l);
        List<String> p = new ArrayList<String>();
        
        if(lp == null) {
            return null;
        }

        for (int i = 0; i < lp.size(); i++){
            p.add(lp.get(i).getNome());
        }

        return p;
    }
    
    @WebMethod(operationName = "recuperaDettagliProdotto")
    public List<String> recuperaDettagliProdotto(@WebParam(name = "locale") String locale, @WebParam(name = "prodotto") String prodotto) {
        Locale l = localeBean.find(locale);
        Prodotto p = ProdottoBean.findRestaurantProduct(prodotto, l);
    
        List<String> lista = new ArrayList<String>();
        
        lista.add(p.getNome());
        lista.add(p.getCategoria().getNome());
        lista.add(p.getPrezzo() + "");
        lista.add(p.getDescrizione());
        lista.add(p.getUrlimmagine());
    
        return lista;
    }
    
}
